package com.moon.game.command.guest.mainland;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.resource.model.MainLand;
import com.moon.game.service.MainLandService;
import com.moon.game.vos.ErrorMsg;

/**
 * @description 查看大陆详细信息
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-5-26 下午3:11:33
 */
public class ViewMainLand extends BaseCmd{
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
		}
		int mainlandId = Integer.parseInt(ps[0]);
		MainLand mainland = MainLandService.getMainLand(mainlandId);
		if (mainland == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_SYS, "您当前位置大陆不存在！"));
			return;
		}
		req.setVO("name", mainland.getName());
		req.setVO("description", mainland.getDescription());
		Map<String, String> portsMap = new HashMap<String, String>();
		final ArrayList<Integer> ports1 = MainLandService.getPorts(mainlandId);
		if (ports1 != null) {
			for (Integer id:ports1) {
				portsMap.put(String.valueOf(id), MainLandService.getMainLand(id).getName());
			}
		}
		List<MainLand> protLands = new ArrayList<MainLand>();
		final ArrayList<Integer> ports = MainLandService.getPorts(mainlandId);
		if (ports != null) {
			for (Integer id:ports) {
				protLands.add(MainLandService.getMainLand(id));
			}
		}
		req.setVO("ports_map", protLands);
		response(req, req.getClientResponse());
	}

}
